import { _decorator, Component, Node, tween, v3, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('EnergyUseController')
export class EnergyUseController extends Component {

    public energyUseEndPos: Vec3 = Vec3.ZERO;
    public energyUseStartPos: Vec3 = Vec3.ZERO;
    public isNewEndPos: boolean = false;

    public distance:number = 50;

    start() {
        const node = this.node;
        const jumpDuration = 0.1; // 上升和下降各占一半时间
        const height = 50;

        let startPos = v3(this.distance / 2, height, 0);
        if (this.isNewEndPos) {
            startPos = this.energyUseStartPos;
        }

        let endPos = v3(this.distance / 2, -height, 0);
        if (this.isNewEndPos) {
            endPos = this.energyUseEndPos;
        }
        

        // 上升阶段
        tween(node)
            .by(jumpDuration, {
                position: startPos,
            }, { easing: 'sineOut' }) // 使用缓动函数使上升更自然
            .call(() => {
                // 下降阶段
                tween(node)
                    .by(jumpDuration, {
                        position: endPos,
                    }, { easing: 'sineIn' })
                    .start();
            })
            .start();

        let myThis = this;
        this.scheduleOnce(function () {
            myThis.node.destroy();
        }.bind(this), 0.28);
    }

}


